using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace MD3Loader
{
  public class CRenderer
  {
    static Effect simple_effect;

    public static void loadEffect(ContentManager content, string EffectName, GraphicsDevice device)
    {
        simple_effect = content.Load<Effect>(EffectName);
    }

    public static void renderBoundingBox(GraphicsDevice device, Matrix view, Matrix projection, Matrix world, BoundingBox bb)
    {
      VertexPositionColor[] vertexList = new VertexPositionColor[8];
      vertexList[0].Position = bb.Min;
      vertexList[0].Color = Color.Red;
      vertexList[1].Position = new Vector3(bb.Min.X, bb.Min.Y, bb.Max.Z);
      vertexList[1].Color = Color.Red;
      vertexList[2].Position = new Vector3(bb.Min.X, bb.Max.Y, bb.Min.Z);
      vertexList[2].Color = Color.Red;
      vertexList[3].Position = new Vector3(bb.Max.X, bb.Min.Y, bb.Min.Z);
      vertexList[3].Color = Color.Red;
      vertexList[4].Position = new Vector3(bb.Min.X, bb.Max.Y, bb.Max.Z);
      vertexList[4].Color = Color.Red;
      vertexList[5].Position = new Vector3(bb.Max.X, bb.Max.Y, bb.Min.Z);
      vertexList[5].Color = Color.Red;
      vertexList[6].Position = new Vector3(bb.Max.X, bb.Min.Y, bb.Max.Z);
      vertexList[6].Color = Color.Red;
      vertexList[7].Position = bb.Max;
      vertexList[7].Color = Color.Red;

      int[] indices = { 0,1,0,2,0,3,1,4,1,6,2,4,2,5,3,5,3,6,4,7,5,7,6,7 };

      VertexDeclaration decl = new VertexDeclaration(device, VertexPositionColor.VertexElements);

      device.VertexDeclaration = decl;

      simple_effect.Parameters["World"].SetValue(world);
      simple_effect.Parameters["View"].SetValue(view);
      simple_effect.Parameters["Projection"].SetValue(projection);

      simple_effect.Begin();
      foreach (EffectPass pass in simple_effect.CurrentTechnique.Passes)
      {
        pass.Begin();
        device.DrawUserIndexedPrimitives<VertexPositionColor>(
            PrimitiveType.LineList,
            vertexList,
            0,  // vertex buffer offset to add to each element of the index buffer
            8,  // number of vertices in pointList
            indices,
            0,
            12
        );
        pass.End();
      }
      simple_effect.End();
    }

    public static void renderAxis(GraphicsDevice device, Matrix view, Matrix projection, Matrix world, float size)
    {
      world = Matrix.CreateScale(size) * world;

      VertexPositionColor[] pointList = new VertexPositionColor[6];
      pointList[0].Position = new Vector3(0.0f, 0.0f, 0.0f);
      pointList[0].Color = Color.Red;
      pointList[1].Position = new Vector3(1.0f, 0.0f, 0.0f);
      pointList[1].Color = Color.Red;
      pointList[2].Position = new Vector3(0.0f, 0.0f, 0.0f);
      pointList[2].Color = Color.Green;
      pointList[3].Position = new Vector3(0.0f, 1.0f, 0.0f);
      pointList[3].Color = Color.Green;
      pointList[4].Position = new Vector3(0.0f, 0.0f, 0.0f);
      pointList[4].Color = Color.Blue;
      pointList[5].Position = new Vector3(0.0f, 0.0f, 1.0f);
      pointList[5].Color = Color.Blue;

      VertexDeclaration decl = new VertexDeclaration(device, VertexPositionColor.VertexElements);

      device.VertexDeclaration = decl;

      simple_effect.Parameters["World"].SetValue(world);
      simple_effect.Parameters["View"].SetValue(view);
      simple_effect.Parameters["Projection"].SetValue(projection);

      simple_effect.Begin();
      foreach (EffectPass pass in simple_effect.CurrentTechnique.Passes)
      {
        pass.Begin();
        device.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.LineList,
            pointList,
            0,  // vertex buffer offset to add to each element of the index buffer
            3  // number of vertices in pointList
        );
        pass.End();
      }
      simple_effect.End();
    }
  }
}
